package com.sykj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sykj.config.WeChatConfig;
import com.sykj.dto.OrderMasterDto;
import com.sykj.entity.OrderDetail;
import com.sykj.entity.OrderMaster;
import com.sykj.entity.Result;
import com.sykj.service.IOrderDetailService;
import com.sykj.service.IOrderMasterService;
import com.sykj.util.HttpClientUtils;
import com.sykj.util.WeChatUtils;
import com.sykj.util.ResultUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sykj
 * @since 2019-06-28
 */
@RestController
@RequestMapping("/orderMaster")
public class OrderMasterController {

    @Autowired
    private IOrderMasterService masterService;
    
    @Autowired
    private IOrderDetailService detailService;

    @Autowired
    private WeChatConfig config;

    @PostMapping("/createOrder")
    public Result createOrder(@RequestBody OrderMasterDto masterDto) throws IOException {
        String orderId = masterService.createOrder(masterDto);
        // 拼接微信登录url
        String url = WeChatUtils.getLoginUrl(orderId, config.getRedirectUrl());
        // 返回登录url
        return ResultUtil.success("成功", url);
    }

    /**
     * 取消订单
     * @Author vains
     * @Date 2019/6/30 22:38
     * @param orderId 订单编号
     * @return com.sykj.entity.Result
     **/
    @PostMapping("/cancelOrder/{orderId}")
    public Result cancelOrder(@PathVariable("orderId") String orderId) {
        OrderMaster master = masterService.getById(orderId);
        System.out.println(master.getOrderAmount().toBigInteger());
        WeChatUtils.cancel(String.valueOf(master.getOrderAmount().multiply(new BigDecimal(100)).toBigInteger()), orderId);
        // 设置订单状态为退款状态
        master.setOrderStatus(1);
        masterService.updateById(master);
        return ResultUtil.success("退款成功");
    }

    /**
     * 查询订单以及订单详情
     * @Author vains
     * @Date 2019/7/1 0:01 
     * @param orderId
     * @return com.sykj.entity.Result
     **/
    @PostMapping("/getById/{orderId}")
    public Result getById(@PathVariable("orderId") String orderId) {
        // 获取订单对象
        OrderMaster master = masterService.getById(orderId);
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        // 根据订单编号获取对应商品
        List<OrderDetail> details = detailService.list(wrapper);
        OrderMasterDto masterDto = new OrderMasterDto();
        // 将数据copy到传输对象中
        BeanUtils.copyProperties(master, masterDto);
        masterDto.setOrderDetails(details);
        String userInfo = WeChatUtils.getUserInfo(masterDto.getBuyerOpenid());
        Map<String, Object> map = new HashMap<>();
        Vector v = new Vector();
        map.put("order", masterDto);
        map.put("headImage", WeChatUtils.getHeadImage(userInfo));
        return ResultUtil.success("查询成功", map);
    }

}
